<?php
class M_ACL_User extends K_MVC_Model_Base {

    private $_arData = array(
        'login' => '@guest',
        'mail' => null,
        'password' => null,
        'privilege' => null
    );


    private $_objAccess = null;


    public function athorization (M_ACL_AthorizationData $objData) {
        if ($this->getLogin() != '@guest') {
            return false;
        }

        $arAccount = $this->_getAccount($objData);
        if (!empty($arAccount)) {
            $this->_arData = $arAccount;
            return true;
        }

        return false;
    }


    public function getLogin () {
        return $this->_arData['login'];
    }


    public function getMail () {
        return $this->_arData['mail'];
    }


    public function getPrivilege () {
        return $this->_arData['privilege'];
    }


    public function getAccess() {
        if (empty($this->_objAccess)) {
            $this->_objAccess = $this->getModel('ACL_Access');
        }
        return $this->_objAccess;
    }


    private function _getAccount (M_ACL_AthorizationData $objData) {
        $objDb = $this->_getDb('ACL');

        $sqlGetAccount = <<<SQL
SELECT *
  FROM Acp_Users
  WHERE login = ? AND password = ?
SQL;

        return $objDb->selectRow($sqlGetAccount, $objData->getLogin(), $objData->getPassword());
    }


    public function isCorrectAuthorizationData($strLogin, $strPassword) {
        $objDb = $this->_getDb('ACL');

        $sqlIsExists = <<<SQL
SELECT COUNT(*)
  FROM Acp_Users
  WHERE login = ? AND password = MD5(?)
SQL;

        return (boolean) $objDb->selectCell($sqlIsExists, $strLogin, $strPassword);

    }

}